package com.rui.study.algorithm.T_集合遍历过程中增加元素;

import java.util.*;

/**
 * 递归遍历元素，查找所有父节点
 *
 * @author yaowr
 * 2019-04-28
 */
public class CollectionTEst {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        Set<String> set = new HashSet<>(list);
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
            String parent = getParent(s);
            if (parent != null && parent.length() > 0) {
                if (set.add(parent)) {
                    list.add(parent);
                }
            }
        }
        System.out.println(Arrays.toString(set.toArray()));
    }

    private static String getParent(String s) {
        String parent = null;
        if (s.equals("b")) {
            parent = "d";
        }
        if (s.equals("d")) {
            parent = "b";
        }
        if (s.equals("c")) {
            parent = "e";
        }
        return parent;
    }
}
